package org.xq.softcup.mq.registry.impl;

import org.xq.softcup.mq.registry.ServiceRegistry;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * 本地服务注册, 单机服务注册
 */
public class LocalServiceRegistry extends ServiceRegistry {
    private Map<String, TreeSet<String>> registryData;

    public void start(Map<String, String> param) {
        this.registryData = new HashMap();
    }

    public void stop() {
        this.registryData.clear();
    }

    public boolean registry(Set<String> keys, String value) {
        if ((keys == null) || (keys.size() == 0) || (value == null) || (value.trim().length() == 0)) {
            return false;
        }
        for (String key : keys) {
            TreeSet<String> values = (TreeSet) this.registryData.get(key);
            if (values == null) {
                values = new TreeSet();
                this.registryData.put(key, values);
            }
            values.add(value);
        }
        return true;
    }

    public boolean remove(Set<String> keys, String value) {
        if ((keys == null) || (keys.size() == 0) || (value == null) || (value.trim().length() == 0)) {
            return false;
        }
        for (String key : keys) {
            TreeSet<String> values = (TreeSet) this.registryData.get(key);
            if (values != null) {
                values.remove(value);
            }
        }
        return true;
    }

    public Map<String, TreeSet<String>> discovery(Set<String> keys) {
        if ((keys == null) || (keys.size() == 0)) {
            return null;
        }
        Map<String, TreeSet<String>> registryDataTmp = new HashMap();
        for (String key : keys) {
            TreeSet<String> valueSetTmp = discovery(key);
            if (valueSetTmp != null) {
                registryDataTmp.put(key, valueSetTmp);
            }
        }
        return registryDataTmp;
    }

    public TreeSet<String> discovery(String key) {
        return (TreeSet) this.registryData.get(key);
    }
}
